from manim import *


class move_curve_draw_rectangle(Scene):
    def draw_oring_rectangle(self, ax, x, y):
        points = [
            ax.c2p(0, 0).tolist(),
            ax.c2p(x, 0).tolist(),
            ax.c2p(x, y).tolist(),
            ax.c2p(0, y).tolist(),
        ]
        polygon = Polygon(
            *points,
            fill_color=GREEN,
            fill_opacity=0.5,
            stroke_color=WHITE,
            stroke_width=2,
        )
        return polygon

    def construct(self):
        ax = Axes(
            x_range=[0, 10, 1],
            y_range=[0, 10, 1],
            x_length=6,
            y_length=6,
            axis_config={"include_tip": False},
        )
        t = ValueTracker(1)

        def func_divider(x):
            return 10 / x

        graph = ax.plot(
            func_divider,
            color=YELLOW,
            x_range=[1, 10, 0.1],
            use_smoothing=False,
        )

        self.add(ax, graph)

        dot = Dot()
        dot.add_updater(
            lambda x: x.move_to(ax.c2p(t.get_value(), func_divider(t.get_value())))
        )
        dot.set_z_index(1)
        self.play(Create(dot))

        points = [
            ax.c2p(0, 0).tolist(),
            ax.c2p(1, 0).tolist(),
            ax.c2p(1, 10).tolist(),
            ax.c2p(0, 10).tolist(),
        ]

        poly = Polygon(
            *points,
            fill_color=GREEN,
            fill_opacity=0.5,
            stroke_color=WHITE,
            stroke_width=2,
        )
        poly.add_updater(
            lambda x: x.become(
                self.draw_oring_rectangle(
                    ax, t.get_value(), func_divider(t.get_value())
                )
            )
        )
        self.play(Create(poly))
        self.play(t.animate.set_value(5), run_time=5)
        self.play(t.animate.set_value(10), run_time=5)
